﻿using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FileManagerGlobal_Library;
using FileManager_Client.BusinessLogic;
using FileManager_Client.BusinessLogic.FileManagerUserService;
using FileManager_Client.BusinessLogic.FileTransferService;
using FileManager_Client.PresentationLayer.Common;
using FileManager_Client.PresentationLayer.Properties;
using FileManager_Client.Utility;
using System.Collections.Generic;
using System.Drawing;
using BrightIdeasSoftware;
using System;

namespace FileManager_Client.PresentationLayer.NewUi
{
    public partial class FileManager_Form
    {
        #region Uploading File Shown Event And Init Object
        //当文件开始下载时，把分包对象全部加入到当前集合中
        private Dictionary<Guid, List<UploadFileProgress>> SuccessFileUpload =
            new Dictionary<Guid, List<UploadFileProgress>>();

        //每个文件所对应的所有线程
        private Dictionary<Guid, List<BackgroundWorker>> FileUploadThreadCollection =
            new Dictionary<Guid, List<BackgroundWorker>>();

        public List<UploadFileProgress> UploadingFileCollection = new List<UploadFileProgress>();
        private static object LockUploadingUI = new object();
        private static object LockFile = new object();
        private List<string> BreakUploadingList = new List<string>(); 
        void FileManager_Form_Shown_UploadingBreak(object sender, EventArgs e)
        {
            var breakObjectCollection = UserLocalFile.GetFileUploadingBreakObject();
            var tooBarItem = ToolBar_System.Items[ToolBarItemIndex.UploadManager];
            foreach (var fileUploadingBreakClass in breakObjectCollection)
            {
                tooBarItem.LeftUpNumber++;
                var breakObject = new UploadFileProgress
                {
                    BeginTime = fileUploadingBreakClass.BeginTime,
                    CurrentState = fileUploadingBreakClass.CurrentState,
                    DisplaySize = fileUploadingBreakClass.DisplaySize,
                    FileGuid = fileUploadingBreakClass.FileGuid,
                    FileLocalPath = fileUploadingBreakClass.FileLocalPath,
                    FileName = fileUploadingBreakClass.FileName,
                    FileSize = fileUploadingBreakClass.FileSize,
                    FolderId = fileUploadingBreakClass.FolderId,
                    ServerPath = fileUploadingBreakClass.ServerPath,
                    UploadingState = fileUploadingBreakClass.UploadingState
                };
                var simpleButton = new SimpleButton { Enabled = true, Text = "继续上传" };
                breakObject.UploadButton = simpleButton;
                simpleButton.Click += simpleButton_Click;
                simpleButton.Tag = breakObject.FileGuid;
                //var progressBar = new System.Windows.Forms.ProgressBar { Maximum = fileUploadingBreakClass.MaxProgress, Value = fileUploadingBreakClass.ValueProgress };
                var progressBar = new ProgressBarControl();
                progressBar.Properties.Maximum = fileUploadingBreakClass.MaxProgress;
                progressBar.Position = fileUploadingBreakClass.ValueProgress;
                progressBar.Properties.PercentView = true;
                progressBar.Properties.ShowTitle = true;
                breakObject.UploadProgressBar = progressBar;
                Olv_Uploading.AddObject(breakObject);
                breakObject.UploadingClass = new List<FileInfoLocalClass>();
                var maxValue = 0;
                foreach (var fileUploadingBreakBlockClass in fileUploadingBreakClass.FileUploadingBreakBlockClasses)
                {
                    breakObject.UploadingClass.Add(new FileInfoLocalClass
                    {
                        FileEndOffset = fileUploadingBreakBlockClass.FileOffset + fileUploadingBreakBlockClass.TransferLength,
                        FileGuid = fileUploadingBreakBlockClass.FileGuid,
                        FileOffset = fileUploadingBreakBlockClass.FileOffset,
                        FileServerPath = fileUploadingBreakBlockClass.FileServerPath,
                        LocalPath = fileUploadingBreakBlockClass.LocalPath,
                        TransferLength = fileUploadingBreakBlockClass.TransferLength
                    });
                }
                UploadingFileCollection.Add(breakObject);
                foreach (var item in Olv_Uploading.Items.Cast<ListViewItem>().Where(item => string.IsNullOrEmpty(item.Name)))
                {
                    var uploadfileObj =
                        UploadingFileCollection.FirstOrDefault(
                            x => x.FileName.Equals(item.Text));
                    if (null == uploadfileObj) continue;
                    Olv_Uploading.AddEmbeddedControl(uploadfileObj.UploadProgressBar, 2, item.Index);
                    Olv_Uploading.AddEmbeddedControl(uploadfileObj.UploadButton, 5, item.Index, DockStyle.None);
                    //item.ImageIndex = 1;//ReturnImageKey(uploadfileObj.FileName);
                    item.Name = uploadfileObj.FileGuid;
                }
            }
        }
        #endregion

        #region Add File To Uploading
        private void AddUploadFile(List<UploadFileProgress> uploadFileProgresses)
        {
            var startUploadingWorker = new BackgroundWorker();
            startUploadingWorker.DoWork += startUploadingWorker_DoWork;
            startUploadingWorker.RunWorkerAsync(uploadFileProgresses);
        }

        private void startUploadingWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var tooBarItem = ToolBar_System.Items[ToolBarItemIndex.UploadManager];
                var uploadFileProgresses = e.Argument as List<UploadFileProgress>;
                if (null == uploadFileProgresses) return;
                UploadingFileCollection.AddRange(uploadFileProgresses);
                foreach (var fileProgress in uploadFileProgresses)
                {
                    tooBarItem.LeftUpNumber++;
                    var simpleButton = new SimpleButton { Enabled = false, Text = "暂停上传" };
                    fileProgress.UploadButton = simpleButton;
                    simpleButton.Click += simpleButton_Click;
                    simpleButton.Tag = fileProgress.FileGuid;
                    var progressBar = new ProgressBarControl();
                  //  var progressBar = new System.Windows.Forms.ProgressBar { Value = 0 };
                    progressBar.Properties.PercentView = true;
                    progressBar.Properties.ShowTitle = true;
                    fileProgress.UploadProgressBar = progressBar;
                    lock (LockUploadingUI)
                    {
                        Olv_Uploading.Invoke((EventHandler)delegate
                        {
                            Olv_Uploading.AddObject(fileProgress);
                        });
                    }
                }
                lock (uploadFileProgresses)
                {
                    Olv_Uploading.Invoke((EventHandler)delegate
                    {
                        foreach (var item in Olv_Uploading.Items.Cast<ListViewItem>().Where(item => string.IsNullOrEmpty(item.Name)))
                        {
                            var uploadfileObj =
                                uploadFileProgresses.FirstOrDefault(
                                    x => x.FileName.Equals(item.Text));
                            if (null == uploadfileObj) continue;
                            SetUploadingList(uploadfileObj);
                            Olv_Uploading.AddEmbeddedControl(uploadfileObj.UploadProgressBar, 2, item.Index);
                            Olv_Uploading.AddEmbeddedControl(uploadfileObj.UploadButton, 5, item.Index, DockStyle.None);
                            //item.ImageIndex = 1;//ReturnImageKey(uploadfileObj.FileName);
                            item.Name = uploadfileObj.FileGuid;
                        }
                    });
                }
                lock (LockUploadingUI)
                {
                    Olv_Uploading.Invoke((EventHandler)delegate { Olv_Uploading.Update(); });
                }
                var hasFileName = new List<string>();
                foreach (var fileProgress in uploadFileProgresses)
                {
                    var args = new CheckFileUploadingArgs
                    {
                        FileLengthk__BackingField = fileProgress.FileSize,
                        FileNamek__BackingField = fileProgress.FileName,
                        FolderIdk__BackingField = fileProgress.FolderId,
                        IsOverUploadk__BackingField = true,
                        UserIdk__BackingField = userSession.UserIdk__BackingField
                    };
                    var result = fileService.CheckFile(args);
                    fileProgress.ServerPath = result.ServerFilePathk__BackingField;
                    if (result.EnumResultk__BackingField == FileCheckEnum.CreateFileSuccess)
                    {
                        foreach (var fileInfoLocalClass in fileProgress.UploadingClass)
                        {
                            fileInfoLocalClass.FileServerPath = result.ServerFilePathk__BackingField;
                            fileInfoLocalClass.FileEndOffset = fileInfoLocalClass.FileOffset + fileInfoLocalClass.TransferLength;
                            var uploadingWorker = new BackgroundWorker();
                            fileInfoLocalClass.BackgroundWorker = uploadingWorker;
                            uploadingWorker.DoWork += uploadingWorker_DoWork;
                            uploadingWorker.WorkerSupportsCancellation = true;
                            uploadingWorker.RunWorkerAsync(fileInfoLocalClass);
                        }
                    }
                    else
                    {
                        UploadingFileCollection.Remove(fileProgress);
                        Olv_Uploading.RemoveObject(fileProgress);
                        tooBarItem.LeftUpNumber--;
                        hasFileName.Add(fileProgress.FileName);
                    }
                }
                if (hasFileName.Count > 0)
                {
                    var messageStr = hasFileName.Aggregate("以下文件服务正在上传，请稍后操作!", (current, temp) => current + ("\r\n" + temp));
                    XtraMessageBox.Show(messageStr, GlobalString.System_Config_String.MbxInfoStr, MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                Olv_Uploading.Invoke((EventHandler)delegate
                {
                    Olv_Uploading.Refresh();
                    Olv_Uploading.Update();
                });
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("startUploadingWorker_DoWork has problem", ex); 
            }
        }

        private static long PerMb = (70*1024*1024);
        private static long LowMb = (2*1024);
        private void SetUploadingList(UploadFileProgress uploadfileObj)
        {
            uploadfileObj.UploadingClass = new List<FileInfoLocalClass>();
            //Multiplied by 5 threads per 100mb
            var threadCount = GlobalVariable.ClientAppVariable.UploadNumberOfThreads;
            if (uploadfileObj.FileSize < LowMb)
            {
                threadCount = 2;
            }
            if(uploadfileObj.FileSize  > PerMb)
            {
                 var perount = (int)(uploadfileObj.FileSize/PerMb);
                threadCount +=perount;
                if (uploadfileObj.FileSize % PerMb > 0)
                    threadCount++;
            }
            var transferLength = uploadfileObj.FileSize / threadCount;
            var progressMax = 0;
            for (var i = 0; i < threadCount; i++)
            {
                var tempClass = new FileInfoLocalClass()
                {
                    FileOffset = i * transferLength,
                    LocalPath = uploadfileObj.FileLocalPath,
                    FileGuid = uploadfileObj.FileGuid
                };
                if (i + 1 == threadCount)
                    tempClass.TransferLength = (transferLength + uploadfileObj.FileSize % threadCount);
                else
                    tempClass.TransferLength = transferLength;
                progressMax += (int)(tempClass.TransferLength / GlobalVariable.ClientAppVariable.UploadBlockSize);
                if (tempClass.TransferLength % GlobalVariable.ClientAppVariable.UploadBlockSize > 0)
                    progressMax++;
                uploadfileObj.UploadingClass.Add(tempClass);
            }
            uploadfileObj.UploadProgressBar.Properties.Maximum = progressMax;
        }

        private void uploadingWorker_DoWork(object sender, DoWorkEventArgs e)
        {
            try
            {
                var uploadClass = e.Argument as FileInfoLocalClass;
                if (null == uploadClass) return;
                var fileUploadingServer = ServiceBroker.CreateFileServer();
                var backgroundWorker = (sender as BackgroundWorker);
                if (null == backgroundWorker) return;
                var fileStream = File.OpenRead(uploadClass.LocalPath);
                var upload = new UploadFileInfo
                {
                    Length = uploadClass.FileEndOffset - uploadClass.FileOffset,
                    Name = uploadClass.FileServerPath,
                    Offset = uploadClass.FileOffset,
                    UploadSize = 0
                };
                var fileUpload = UploadingFileCollection.FirstOrDefault(x => x.FileGuid.Equals(uploadClass.FileGuid));
                fileUpload.CurrentState = "正在上传...";
                Olv_Uploading.Invoke((EventHandler)delegate
                {
                    Olv_Uploading.RefreshObject(fileUpload);
                });
                if (fileUpload.UploadingState != UploadingStateEnum.Uploading)
                    fileUpload.UploadingState = UploadingStateEnum.Uploading;
                if (!fileUpload.UploadButton.Enabled)
                {

                    fileUpload.UploadButton.Invoke((EventHandler)delegate
                    {
                        fileUpload.UploadButton.Enabled = true;
                        fileUpload.UploadButton.Update();
                    });
                }
                while (upload.Length != upload.UploadSize)
                {
                    upload.Data = new byte[upload.Length - upload.UploadSize <= GlobalVariable.ClientAppVariable.UploadBlockSize ? upload.Length - upload.UploadSize : GlobalVariable.ClientAppVariable.UploadBlockSize]; //设置传递的数据的大小
                    fileStream.Position = upload.Offset; //设置本地文件数据的读取位置
                    fileStream.Read(upload.Data, 0, upload.Data.Length);//把数据写入到file.Data中
                    fileUploadingServer.UplodaFile(upload);     //上传
                    upload.Offset += upload.Data.Length;
                    upload.UploadSize += upload.Data.Length;
                    uploadClass.FileOffset = upload.Offset;
                    uploadClass.TransferLength -= upload.Data.Length;
                    SetProgressAndEnd(uploadClass);
                    if (!backgroundWorker.CancellationPending) continue;
                    if (uploadClass.TransferLength == upload.UploadSize) continue;
                    BreakFileUploading(uploadClass.FileGuid);
                    e.Cancel = true;
                    break;
                }
                fileStream.Close();
                ServiceBroker.DisposeService(fileUploadingServer);
            }
            catch (Exception ex)
            {
                LogLoader.Instance.Log.Error("uploadingWorker_DoWork has problem", ex); 
            }
        }

        private void SetProgressAndEnd(FileInfoLocalClass uploadClass)
        {
            var fileUpload = UploadingFileCollection.FirstOrDefault(x => x.FileGuid.Equals(uploadClass.FileGuid));
            if (null != fileUpload)
            {
                fileUpload.UploadProgressBar.Invoke((EventHandler)
                    delegate
                    {
                        if (fileUpload.UploadProgressBar.Position + 1 <= fileUpload.UploadProgressBar.Properties.Maximum)
                            fileUpload.UploadProgressBar.Position += 1;
                        fileUpload.UploadProgressBar.Update();
                        fileUpload.UploadProgressBar.Invalidate();
                        if (fileUpload.UploadProgressBar.Position != fileUpload.UploadProgressBar.Properties.Maximum) return;
                        //filename
                        fileUpload.CurrentState = "上传完成";
                        Olv_Uploading.RefreshObject(fileUpload);
                        fileUpload.UploadButton.Invoke(
                            (EventHandler)delegate
                            {
                                fileUpload.
                                    UploadButton.
                                    Text = "上传完成";
                                fileUpload.
                                    UploadButton.
                                    Enabled =
                                    false;
                                fileUpload.UploadButton.Update();
                            });
                        var fileManger = ServiceBroker.CreateFileServer();
                        var uploadingSueccss = new UploadingSuccessArgs
                        {
                            FileLengthk__BackingField = fileUpload.FileSize / 1024,
                            FileNamek__BackingField = fileUpload.FileName,
                            FileSidk__BackingField = fileUpload.FileGuid,
                            FolderIdk__BackingField = fileUpload.FolderId,
                            RenameFilePathk__BackingField = fileUpload.ServerPath,
                            UserIdk__BackingField = userSession.UserIdk__BackingField
                        };
                        fileManger.ReNameFile(uploadingSueccss);
                        var tooBarItem = ToolBar_System.Items[ToolBarItemIndex.UploadManager];
                        if (tooBarItem.LeftUpNumber - 1 == 0)
                            tooBarItem.LeftUpNumber = 0;
                        else
                            tooBarItem.LeftUpNumber--;
                        Olv_Uploading.RemoveObject(fileUpload);
                        UploadingFileCollection.Remove(fileUpload);
                        AddUploadLog(fileUpload);
                        UpdateListView(fileUpload.FolderId);
                    });
            }
        }
        #endregion

        #region Uploading Button Event
        public void simpleButton_Click(object sender, EventArgs e)
        {
            var fileGuid = ((SimpleButton)sender).Tag.ToString();
            // var uploadObjects = Olv_Uploading.Objects as IEnumerable<UploadFileProgress>;
            var chooseObj = UploadingFileCollection.FirstOrDefault(x => x.FileGuid.Equals(fileGuid));
            if (null == chooseObj) return;
            switch (chooseObj.UploadingState)
            {
                case UploadingStateEnum.Uploading:
                    chooseObj.CurrentState = "暂停上传....";
                    chooseObj.UploadButton.Enabled = false;
                    chooseObj.UploadButton.Text = "正在停止";
                    chooseObj.UploadingState = UploadingStateEnum.BreakUploading;
                    chooseObj.UploadButton.Update();
                    Olv_Uploading.RefreshObject(chooseObj);
                    foreach (var fileInfoLocalClass in chooseObj.UploadingClass.Where(fileInfoLocalClass => null != fileInfoLocalClass.BackgroundWorker && fileInfoLocalClass.BackgroundWorker.IsBusy))
                        fileInfoLocalClass.BackgroundWorker.CancelAsync();
                    break;
                case UploadingStateEnum.BreakUploading:
                    UserLocalFile.RemoveFileUploadingBreakObject(chooseObj.FileGuid);
                    chooseObj.CurrentState = "正在重新连接....";
                    chooseObj.UploadButton.Enabled = false;
                    chooseObj.UploadButton.Text = "正在连接";
                    chooseObj.UploadingState = UploadingStateEnum.Uploading;
                    chooseObj.UploadButton.Update();
                    Olv_Uploading.RefreshObject(chooseObj);
                    if (!File.Exists(chooseObj.FileLocalPath))
                    {
                        var messageStr = string.Format("文件 {0} 不存在，请检查后再重新上传!", chooseObj.FileName);
                        XtraMessageBox.Show(messageStr, GlobalString.System_Config_String.MbxInfoStr, MessageBoxButtons.OK, MessageBoxIcon.Information);
                        fileService.DeleteUploadingFile(chooseObj.ServerPath);
                    }
                    else
                    {
                        //chooseObj.CurrentState = "正在上传...";
                        chooseObj.UploadButton.Text = "暂停上传";
                        //chooseObj.UploadButton.Enabled = true;
                        chooseObj.UploadButton.Update();
                        //Olv_Uploading.RefreshObject(chooseObj);
                        foreach (var fileInfoLocalClass in chooseObj.UploadingClass.Where(x => x.FileOffset != x.FileEndOffset))
                        {
                            var uploadingWorker = new BackgroundWorker();
                            fileInfoLocalClass.BackgroundWorker = uploadingWorker;
                            uploadingWorker.DoWork += uploadingWorker_DoWork;
                            uploadingWorker.WorkerSupportsCancellation = true;
                            uploadingWorker.RunWorkerAsync(fileInfoLocalClass);
                        }
                    }
                    break;
            }
        }
        #endregion

        #region Break Uploading Method
        private void BreakFileUploading(string fileGuid)
        {
            BreakUploadingList.Add(fileGuid);
            
            var stopNumber = BreakUploadingList.Count(x => x.Equals(fileGuid));
            //var uploadObjects = Olv_Uploading.Objects as IEnumerable<UploadFileProgress>;
            var uploadingObj = UploadingFileCollection.FirstOrDefault(x => x.FileGuid.Equals(fileGuid));
            if (null == uploadingObj) return;
            var threadCount = uploadingObj.UploadingClass.Count;
            var hasUploaded = uploadingObj.UploadingClass.Count(x => x.FileOffset == x.FileEndOffset);
            stopNumber += hasUploaded;
            if (stopNumber != threadCount) return;
            var fileBreak = new FileUploadingBreakClass
                                {
                                    BeginTime = uploadingObj.BeginTime,
                                    CurrentState = uploadingObj.CurrentState,
                                    DisplaySize = uploadingObj.DisplaySize,
                                    FileGuid = uploadingObj.FileGuid,
                                    FileLocalPath = uploadingObj.FileLocalPath,
                                    FileName = uploadingObj.FileName,
                                    FileSize = uploadingObj.FileSize,
                                    FolderId = uploadingObj.FolderId,
                                    MaxProgress = uploadingObj.UploadProgressBar.Properties.Maximum,
                                    ServerPath = uploadingObj.ServerPath,
                                    UploadingState = uploadingObj.UploadingState,
                                    ValueProgress = uploadingObj.UploadProgressBar.Position,
                                    FileUploadingBreakBlockClasses = new List<FileUploadingBreakBlockClass>()
                                };
            foreach (var fileInfoLocalClass in uploadingObj.UploadingClass)
            {
                if(fileInfoLocalClass.FileOffset == fileInfoLocalClass.FileEndOffset)continue;
                fileBreak.FileUploadingBreakBlockClasses.Add(new FileUploadingBreakBlockClass
                                                                 {
                                                                     FileGuid = fileInfoLocalClass.FileGuid,
                                                                     FileOffset = fileInfoLocalClass.FileOffset,
                                                                     FileServerPath = fileInfoLocalClass.FileServerPath,
                                                                     LocalPath = fileInfoLocalClass.LocalPath,
                                                                     TransferLength = fileInfoLocalClass.TransferLength,
                                                                     FileEndOffset = fileInfoLocalClass.FileEndOffset
                                                                 });
            }
            UserLocalFile.SaveFileUploadingBreakObject(fileBreak);
            BreakUploadingList.RemoveAll(x => x.Equals(fileGuid));
            uploadingObj.UploadButton.Invoke((EventHandler) delegate
                                                                {
                                                                    uploadingObj.UploadButton.Text = "继续上传";
                                                                    uploadingObj.UploadButton.Enabled = true;
                                                                    uploadingObj.UploadButton.Update();
                                                                });
        }
        #endregion

        #region Uploading ContextMenu Event
        private void cms_Uploading_Opening(object sender, CancelEventArgs e)
        {
            Tsmi_DeleteUploading.Enabled = null != Olv_Uploading.SelectedItem;
            Tsmi_DeleteAllUploading.Enabled = Olv_Uploading.Items.Count > 0;
        }

        private void Tsmi_DeleteUploading_Click(object sender, EventArgs e)
        {
           
            if (null == Olv_Uploading.SelectedObject) return;
            var selectObj = Olv_Uploading.SelectedObject as UploadFileProgress;
            if (null == selectObj) return;
            if (selectObj.UploadingState == UploadingStateEnum.Uploading)
            {
                XtraMessageBox.Show("选中文件正在上传，请先暂停再删除!", GlobalString.System_Config_String.MbxInfoStr, MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            fileService.DeleteUploadingFile(selectObj.ServerPath);
            Olv_Uploading.RemoveObject(selectObj);
            var delObj = UploadingFileCollection.FirstOrDefault(x => x.FileGuid.Equals(selectObj.FileGuid));
            if (null == delObj) return;
            UploadingFileCollection.Remove(delObj);
            UserLocalFile.RemoveFileUploadingBreakObject(delObj.FileGuid);
            var tooBarItem = ToolBar_System.Items[ToolBarItemIndex.UploadManager];
            tooBarItem.LeftUpNumber--;
        }

        private void Tsmi_DeleteAllUploading_Click(object sender, EventArgs e)
        {
            var tooBarItem = ToolBar_System.Items[ToolBarItemIndex.UploadManager];
            var delObj = new List<UploadFileProgress>();
            foreach (UploadFileProgress del in Olv_Uploading.Objects)
            {
                if (del.UploadingState == UploadingStateEnum.Uploading) continue;
                fileService.DeleteUploadingFile(del.ServerPath);
                UserLocalFile.RemoveFileUploadingBreakObject(del.FileGuid);
                delObj.Add(del);
                UploadingFileCollection.Remove(del);
            }
            foreach (var uploadFileProgress in delObj)
            {
                tooBarItem.LeftUpNumber--;
                Olv_Uploading.RemoveObject(uploadFileProgress);
            }
        }
        #endregion
    }

    #region Uploading Temp Object
    [Serializable]
    public class UploadFileProgress
    {
        public string FileGuid { get; set; }
        public string FileName { get; set; }
        public string FileLocalPath { get; set; }
        public string ServerPath { get; set; }
        public string FolderId { get; set; }
        public DateTime BeginTime { get; set; }
        public UploadingStateEnum UploadingState { get; set; }
        public string CurrentState { get; set; }
        public long FileSize { get; set; }
        public double DisplaySize { get; set; }
        public SimpleButton UploadButton { get; set; }
        public ProgressBarControl UploadProgressBar { get; set; }
        public List<FileInfoLocalClass> UploadingClass { get; set; } 
        //占位
        public string Doing { get; set; }
        public string Progress { get; set; }
    }

    [Serializable]
    public enum UploadingStateEnum
    {
        CreateConnectToServer = 0,
        Uploading =1,
        BreakUploading = 2,
    }

    [Serializable]
    public class FileInfoLocalClass
    {
        public string FileGuid { get; set; }
        public string LocalPath { get; set; }
        public long TransferLength { get; set; }
        public long FileOffset { get; set; } //开始位置
        public long FileEndOffset { get; set; }//结束位置
        public string FileServerPath { get; set; } 
        public BackgroundWorker BackgroundWorker { get; set; }
    }

    #region File Break Class  and Save to local
    [Serializable]
    public class FileUploadingBreakClass
    {
        public string FileGuid { get; set; }
        public string FileName { get; set; }
        public string FileLocalPath { get; set; }
        public string ServerPath { get; set; }
        public string FolderId { get; set; }
        public DateTime BeginTime { get; set; }
        public UploadingStateEnum UploadingState { get; set; }
        public string CurrentState { get; set; }
        public long FileSize { get; set; }
        public double DisplaySize { get; set; }
        public List<FileUploadingBreakBlockClass> FileUploadingBreakBlockClasses { get; set; }
        public int MaxProgress { get; set; }
        public int ValueProgress { get; set; }
    }

    [Serializable]
    public class FileUploadingBreakBlockClass
    {
        public string FileGuid { get; set; }
        public string LocalPath { get; set; }
        public long TransferLength { get; set; }
        public long FileOffset { get; set; } //开始位置
        public long FileEndOffset { get; set; }//结束位置
        public string FileServerPath { get; set; }
    }
    #endregion

    public static class ToolBarItemIndex
    {
        public static int FileManager = 0;
        public static int FileSearch = 1;
        public static int UploadManager = 2;
        public static int DownloadManager = 3;
    }
#endregion
}